<?php
/* empty stubs for now */
function cable_parser_load($connector)
{
	return true;
}

function cable_parser_unload($connector)
{
	return true;
}

/* lib_parse_line settings */
define( LINK_FRONT_DENOTER,		'[');
define( LINK_BACK_DENOTER,		']');
define( PARSE_LINE_DELIMITER,	'^');
/* base URLs for other services */
define(	REMOTE_WIKI_PATH,		'http://en.wikipedia.org/wiki/');
define(	REMOTE_GOOGLE_PATH,		'http://www.google.com/search?q=');
define( REMOTE_DOI_PATH,		'http://dx.doi.org/');
define( REMOTE_DICTIONARY_PATH, 'http://dictionary.reference.com/search?q=');
/* end lib_parse_line */

function cable_parser_open(&$connector)
{
	global $cable_parser_plugin;
	
	/* initialize plugin global */
	$cable_parser_plugin = array();

	/* local path for link includes */
	define( LOCAL_FILE_BASE_PATH,	$connector->config[SitePath].'users/'.$connector->data[author]);
	define(	LOCAL_FILE_BASE_URL,	$connector->config[SiteURL].'users/'.$connector->data[author]);
	define(	LOCAL_EMOT_BASE_URL,	$connector->config[SiteURL].'users/emoticons/');
	define(	LOCAL_RATE_BASE_URL,	$connector->config[SiteURL].'users/rating/');
	
	/* append css file to head */
	$connector->template->AppendInsert('Head','<link type="text/css" href="{I CABLE-Base.url /}css/cable_parser/parser.css" rel="stylesheet" />');
	
	return true;
}

function cable_parser(&$connector)
{
	global $cable_parser_plugin;
	
	$counter = 0;
	
	/* break apart the line into sections delimited by BLOG_PARSE_DELIMITER */
	$string_array = explode( PARSE_LINE_DELIMITER, $connector->data['lines'][$connector->data['line_counter']] );
	
	/* go through each field in the string separated by BLOG_PARSE_DELIMITER */
	if ( count($string_array) > 2)
	{
		for( $counter=0; $counter < count($string_array); $counter++ )
		{
			switch ( strtolower($string_array[$counter]) )
			{
			case 'link':
				/* building the link */
				$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $string_array[$counter +1] . '">' . $string_array[$counter +2]  . '</a>' . LINK_BACK_DENOTER;
				
				/* set the fields we used to null, because at the end we're going to smash everything back down again. */
				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				break;
			case 'image':
			case 'simpleimage':
				$img_URL = LOCAL_FILE_BASE_URL.'/'.rawurlencode($string_array[$counter +1]);
				$img_PATH = LOCAL_FILE_BASE_PATH.'/'.rawurlencode($string_array[$counter +1]);
				
				if (($string_array[$counter +2] > 0) && ($string_array[$counter +3] > 0))
				{
					$width = $string_array[$counter +2];
					$height = $string_array[$counter +3];
				}
				else
				{
					if (file_exists($img_PATH))
					{
						$size = getimagesize($img_PATH);
						$width = $size[0];
						$height = $size[1];
					}
					else
					{
						$width = '*';
						$height= '*';
					}
				}

				$image_string = "<img src=\"$img_URL\" width=\"$width\" height=\"$height\" border=\"0\" alt=\"[" . $string_array[$counter +1] . ']" />';
				if ($string_array[$counter] == 'image')
					$string_array[$counter] = '<div><div class="ImageBox" style="width:' . ($width +10) . 'px" border=\"0\">' . $image_string . '</div></div>';
				else
					$string_array[$counter] = $image_string;

				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				$string_array[$counter +3] = '';
				break;
			case 'wiki':
				//wiki short format
				$wiki_URL = REMOTE_WIKI_PATH . str_replace(' ','_',$string_array[$counter +1]);
				$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $wiki_URL . '">' . $string_array[$counter +1]  . '</a>' . LINK_BACK_DENOTER;
				
				$string_array[$counter +1] = '';
				break;
			case 'wikil':
				//wiki long format
				$wiki_URL = REMOTE_WIKI_PATH . str_replace(' ','_',$string_array[$counter +1]);
				$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $wiki_URL . '">' . $string_array[$counter +2]  . '</a>' . LINK_BACK_DENOTER;
				
				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				break;
			case 'google':
				//google search query
				$google_URL = REMOTE_GOOGLE_PATH . urlencode( $string_array[$counter +1] );
				$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $google_URL . '">' . $string_array[$counter +1]  . '</a>' . LINK_BACK_DENOTER;
				
				$string_array[$counter +1] = '';
				break;
			case 'source':
				$string_array[$counter] = '<sup class="ReferenceLink">[<a href="' . $string_array[$counter +1] . '">' . $string_array[$counter +2]  . '</a>]</sup>';
				
				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				break;
			case 'ref':
			case 'reference':
				$cable_parser_plugin[reference_counter]++;
				$cable_parser_plugin[reference_list][] = $string_array[$counter +1];
				$cable_parser_plugin[reference_list_anchor][] = substr(md5($string_array[$counter +1]),0,5);
				$string_array[$counter] = '<sup class="ReferenceLink">[<a href="#' . $cable_parser_plugin[reference_list_anchor][$cable_parser_plugin[reference_counter] -1] . '">' .  $cable_parser_plugin['reference_counter'] . '</a>]</sup>';
				
				$string_array[$counter +1] = '';
				break;
			case 'dictionary':
				//definition of a word
				$dictionary_URL = REMOTE_DICTIONARY_PATH . urlencode( $string_array[$counter +1]);
				$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $dictionary_URL . '">' . $string_array[$counter +1] . '</a>' . LINK_BACK_DENOTER;
				
				$string_array[$counter +1] = '';
				break;
			case 'doi':
				$doi_URL = REMOTE_DOI_PATH . urlencode( $string_array[$counter+1]);

				$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $doi_URL . '">' . $string_array[$counter +2] . '</a>' . LINK_BACK_DENOTER;
				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				break;
			case 'hr':
				$string_array[$counter] = '<HR NOSHADE SIZE="1" WIDTH="50%" ALIGN="center">';
				break;
			case 'picturelink':
			case 'filelink':
				$picture_URL = LOCAL_FILE_BASE_URL.urlencode( $string_array[$counter +1] );
				$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $picture_URL . '">' . $string_array[$counter +2] . '</a>' . LINK_BACK_DENOTER;
				
				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				break;
			case 'bloglink':
				$blog_URL = BLOG_SCRIPT_URL . $string_array[$counter +1] . '#' . $string_array[$counter +1];
				$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $blog_URL . '">' . $string_array[$counter +2] . '</a>' . LINK_BACK_DENOTER;
			
				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				break;
			case 'emot':
			case 'emoticon':
				$img_URL = LOCAL_EMOT_BASE_URL . urlencode( $string_array[$counter +1] ) . '.gif';
				$string_array[$counter] = '<img src="' . $img_URL . '" alt="[emoticon_' . $string_array[$counter +1] . ']" valign="middle" />';
				
				$string_array[$counter +1] = '';
				break;
			case 'divclassstart':
			case 'divclass':
				$string_array[$counter] = '<div class="' . $string_array[$counter +1] . '">';
				$string_array[$counter +1] = "";
				$cable_parser_plugin[divclassstart]++;
				break;
			case 'divstylestart':
			case 'divstyle':
				$string_array[$counter] = '<div style="' . $string_array[$counter +1] . '">';
				$string_array[$counter +1] = '';
				$cable_parser_plugin[divstylestart]++;
				break;
			case 'divend':
			case 'enddiv':
			case 'divclassend':
			case 'divstyleend':
			case 'endcenter':
				$string_array[$counter] = '</div>';
				$cable_parser_plugin[divend]++;
				break;
			case 'spanstyle':
			case 'spanstylestart':
				$string_array[$counter] = '<span style="' . $string_array[$counter +1] . '">';
				$string_array[$counter +1] = '';
				$cable_parser_plugin[spanstart]++;
				break;
			case 'endspan':
			case 'spanstyleend':
			case 'endspanstyle':
			case 'spanend':
				$string_array[$counter] = '</span>';
				$cable_parser_plugin[spanend]++;
				break;
			case 'center':
				$string_array[$counter] = '<div style="text-align:center">';
				break;
			case 'startletter':
				$string_array[$counter] = '<blockquote><div class="StartLetter">';
				$cable_parser_plugin[startletter]++;
				break;
			case 'endletter':
				$string_array[$counter] = "</div></blockquote>\n";
				$cable_parser_plugin[endletter]++;
				break;
			case 'startquote':
				$string_array[$counter] = '<div class="QuoteBox"><div class="QuoteText">';
				$cable_parser_plugin[startquote]++;
				break;
			case 'endquote':
				if ( (strpos($string_array[$counter +1],'http://') > 0 ) || (strpos($string_array[$counter +1],'http://') === false) )
					$string_array[$counter] = '</div><div class="QuoteSource">' . $string_array[$counter +1] . '</div>';
				else
					$string_array[$counter] = '</div><div class="QuoteSource">' .  LINK_FRONT_DENOTER . '<a href="' . $string_array[$counter +1] . '">source</a>' . LINK_BACK_DENOTER . '</div>';
				$string_array[$counter] = $string_array[$counter] . '</div>';

				$string_array[$counter +1] = '';
				$cable_parser_plugin[endquote]++;
				break;
			case 'color':
				$string_array[$counter] = '<span style="color:' . $string_array[$counter +1] . '">' . $string_array[$counter +2] . '</span>';
				
				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				break;
			case 'caution':
			case 'warning':
				$string_array[$counter] = '<span style="color:red;font-style:italic">' . $string_array[$counter +1] . '</span>';
				
				$string_array[$counter +1] = '';
				break;
			case 'objectionable':
				$string_array[$counter] = '<span style="color:red;font-style:italic">(Caution: Objectionable Material)</span>';
				break;
			case 'badlanguage':
				$string_array[$counter] = '<span style="color:red;font-style:italic">(Caution: Objectionable Language)</span>';
				break;
			case 'badgraphic':
				$string_array[$counter] = '<span style="color:red;font-style:italic">(Caution: Graphic Imagery)</span>';
				break;
			case 'rating':
				$full_image_url = LOCAL_RATE_BASE_URL . $string_array[$counter +3] . '_full.png';
				$half_image_url = LOCAL_RATE_BASE_URL . $string_array[$counter +3] . '_half.png';
				$no_image_url = LOCAL_RATE_BASE_URL . $string_array[$counter +3] . '_no.png';
				$rating_image_width = $string_array[$counter +4];
				$rating_image_height = $string_array[$counter +5];
				$rating_value = $string_array[$counter +1] -1;
				$full_rating_string = '';
				for($i = 0; $i < $string_array[$counter +2]; $i++)
				{
					if ($i <= $rating_value)
							$full_rating_string = $full_rating_string . "<img src=\"$full_image_url\"";
						elseif (($i >  ($rating_value - 0.6)) && ($i < ($rating_value +1)))
							$full_rating_string = $full_rating_string . "<img src=\"$half_image_url\"";
						else
							$full_rating_string = $full_rating_string . "<img src=\"$no_image_url\"";
					$full_rating_string = $full_rating_string . " width=\"$rating_image_width\" height=\"$rating_image_height\" align=\"middle\" />";
				}

				$string_array[$counter] = $full_rating_string;
				$string_array[$counter +1] = '';
				$string_array[$counter +2] = '';
				$string_array[$counter +3] = '';
				$string_array[$counter +4] = '';
				$string_array[$counter +5] = '';
				break;
			default:
				if (substr($string_array[$counter],0,7) == 'http://')
					$string_array[$counter] = LINK_FRONT_DENOTER . '<a href="' . $string_array[$counter] . '">' . urldecode($string_array[$counter])  . '</a>' . LINK_BACK_DENOTER;
			
				break;
			}
		}
		/* put the line pieces back together */
		$connector->data[lines][$connector->data[line_counter]] = implode( '', $string_array );

		return true;
	}
	
	$cable_parser_plugin[linecount]++;
	
	return true;
}

function cable_parser_close(&$connector)
{
	/* recursive function to close open CABLE tags/cards */
	global $cable_parser_plugin;
	
	if (($cable_parser_plugin[divclassstart] + $cable_parser_plugin[$divstylestart]) > $cable_parser_plugin[divend])
	{
		$replace = array('divclassstart or divstylestart','</div>',($cable_parser_plugin[divclassstart] + $cable_parser_plugin[divstylestart]) - $cable_parser_plugin[divend]);

		unset($cable_parser_plugin[divclassstart]);
		unset($cable_parser_plugin[divstylestart]);
	}
	elseif($cable_parser_plugin[spanstart] > $cable_parser_plugin[spanend])
	{
		$replace = array('spanstart','</span>',$cable_parser_plugin[spanstart] - $cable_parser_plugin[spanend]);
		
		unset($cable_parser_plugin[spanstart]);
	}
	elseif($cable_parser_plugin[startletter] > $cable_parser_plugin[endletter])
	{
		$replace = array('startletter','</div></blockquote>',$cable_parser_plugin[startletter] - $cable_parser_plugin[endletter]);
		
		unset($cable_parser_plugin[startletter]);
	}
	elseif($cable_parser_plugin[startquote] > $cable_parser_plugin[endquote])
	{
		$replace = array('startquote','</div></div>',$cable_parser_plugin[startquote] - $cable_parser_plugin[endquote]);
		
		unset($cable_parser_plugin[startquote]);
	}
	else
	{
		/* append the necessary footers/references */
		$connector->data['lines'][] = lib_return_footers();

		/* reset array */
		$cable_parser_plugin = array();
		
		/* we're all done */
		return true;
	}	
	
	/* terminate all tags */
	$connector->data['lines'][] = "<!-- $replace[2] improperly closed $replace[0] tags found -->";
	for($i=0; $i < $replace[2]; $i++)
			$connector->data['lines'][] = $replace[1];
	
	/* recursive call */
	return cable_parser_close($connector);
}

function lib_return_footers()
{
	/* function to build references/footer tables */
	global $cable_parser_plugin;
	$term_text = '';
		
	$ref_text = false;		
	for($i=0; $i < $cable_parser_plugin[reference_counter]; $i++)
	{
		if ($i == 0)
			$ref_text = '<div class="ReferenceBox">References';
		
		if (substr($cable_parser_plugin[reference_list][$i],0,7) == 'http://')
			$ref_text = $ref_text . "\n<a name=\"" . $cable_parser_plugin[reference_list_anchor][$i] . '"> </a><div class="ReferenceText">' . ($i +1) . '. <a href="' . $cable_parser_plugin[reference_list][$i] . '">' . $cable_parser_plugin[reference_list][$i] . "</a></div>\n";
		else
			$ref_text = $ref_text . "\n<a name=\"" . $cable_parser_plugin[reference_list_anchor][$i] . '"> </a><div class="ReferenceText">' . ($i +1) . '. ' . $cable_parser_plugin[reference_list][$i] . "</div>\n";
	}
	if ($ref_text)
		$term_text = $term_text . "$ref_text</div>\n";
	
	unset($cable_parser_plugin);
	return $term_text;
}

?>